package com.quanyan.ic.vendingMachine.mapper.customSQL;

import com.quanyan.ic.common.paging.Limit;
import com.quanyan.ic.vendingMachine.http.request.ReqSalesRecordCondition;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by zhuo.shi on 2017/7/25.
 */
public class VmReportMapperSqlProvider {

    private Logger logger = LoggerFactory.getLogger(VmReportMapperSqlProvider.class);

    public String querySalesRecordList(final ReqSalesRecordCondition condition, Limit limit) {

        StringBuilder selectSQL = new StringBuilder();
        SQL sql = new SQL() {
            {
                SELECT("item.id, " +
                        "pm.place_id AS placeId, pm.place_name AS placeName," +
                        "o.vm_number AS VmNumber, " +
                        "item.grid, " +
                        "item.goods_category AS goodsCategory, " +
                        "item.goods_name AS goodsName, " +
                        "item.goods_price AS goodsPrice, " +
                        "o.buyer, " +
                        "o.pay_way AS payWay, " +
                        "o.pay_status AS payStatus, " +
                        "item.ship_status AS shipStatus, " +
                        "item.qty, " +
                        "item.ship_qty AS shipQty, " +
                        "item.create_time AS createTime");
                FROM("vm_order_item item");
                LEFT_OUTER_JOIN("vm_order o ON(item.order_no=o.order_no)");
                LEFT_OUTER_JOIN("vm_place_mapping pm ON(o.vm_number=pm.vm_number)");
            }
        };

        addConditionSql(sql, condition);
        sql.ORDER_BY(" item.create_time DESC");
        selectSQL.append(sql.toString());

        if (limit != null) {
            selectSQL.append(" limit ");
            selectSQL.append(limit.getOffset());
            selectSQL.append(", ");
            selectSQL.append(limit.getPageSize());
        }
        logger.info("querySalesRecordList customSQL=\n{}", selectSQL.toString());

        return selectSQL.toString();
    }

    public String countSalesRecordList(final ReqSalesRecordCondition condition) {
        StringBuilder countSql = new StringBuilder();
        SQL sql = new SQL() {
            {
                SELECT("count(*)");
                FROM("vm_order_item item");
                LEFT_OUTER_JOIN("vm_order o ON(item.order_no=o.order_no)");
                LEFT_OUTER_JOIN("vm_place_mapping pm ON(o.vm_number=pm.vm_number)");
            }
        };

        addConditionSql(sql, condition);
        countSql.append(sql.toString());

        logger.info("countSalesRecordList customSQL=\n{}", countSql.toString());

        return countSql.toString();
    }

    private void addConditionSql(SQL sql, final ReqSalesRecordCondition condition) {
        sql.WHERE("1=1");
        if (condition.getPlaceId() != null) {
            sql.AND();
            sql.WHERE("pm.place_id=" + condition.getPlaceId());
        }
        if (!StringUtils.isEmpty(condition.getVmNumber())) {
            sql.AND();
            sql.WHERE("o.vm_number='" + condition.getVmNumber() + "'");
        }
        if (!StringUtils.isEmpty(condition.getOrderNo())) {
            sql.AND();
            sql.WHERE("o.order_no='" + condition.getOrderNo() + "'");
        }
        if (!StringUtils.isEmpty(condition.getStartTime())) {
            sql.AND();
            sql.WHERE("item.create_time>='" + condition.getStartTime() + "'");
        }
        if (!StringUtils.isEmpty(condition.getEndTime())) {
            sql.AND();
            sql.WHERE("item.create_time<='" + condition.getEndTime() + "'");
        }
    }
}
